package com.hrt.datasender.datastorage.strategy.monitordata;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.btcode.db.result.Row;
import com.btcode.orm.model.IModel;
import com.hrt.datasender.datadao.model.BcMonitorParamDictModel;
import com.hrt.datasender.datadao.model.BdS5MinutesModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datadao.service.BdSourceService;
import com.hrt.datasender.datadao.service.BdSourceSingleService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.process.strategy.hj212.util.DataType;
import com.hrt.datasender.web.bcmonitorparamdict.BcMonitorParamDictService;
import com.hrt.datasender.web.calSetting.CalSettingService;

import javax.xml.datatype.DatatypeConfigurationException;

public class S5MINUTES extends MonitorDataStrategy {
    private final int dataInterval = 300;
    private final int minuteInterval = 5;

    @Override
    public void registerClearShedule() {
        S5MINUTESClear s5MINUTESc = new S5MINUTESClear();
        s5MINUTESc.registerJob();
    }

    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {
        List<IModel> resultModels = new ArrayList<>();
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        // 获取开始时间与结束时间
        Calendar calStart = Calendar.getInstance();
        calStart.setTime(cal.getTime());
        calStart.add(Calendar.MINUTE, -minuteInterval);

        //数据源
        String dataSource = "BD_SOURCE";

        Double dFlow = getFlowValue(parmList,dataSource,calStart,cal);

        for (int i = 0; i < parmList.size(); i++) {
            BdS5MinutesModel data = new BdS5MinutesModel();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            data.setDataId(parmList.get(i)+sf.format(calStart.getTime()));
            data.setParamCodeBus(parmList.get(i));
            data.setDatatime(calStart.getTime());
            data.setFlag(CalSettingService.getInstance().isCalData(cal.getTime(),parmList.get(i)));
            data.setCreatetime(new Date());

            // 0：当前值 1：算术平均 2：pH平均 3：区间累计
            BcMonitorParamDictModel paramModel = new BcMonitorParamDictModel();
            paramModel.setParamCodeBus(data.getParamCodeBus());
            paramModel = BcMonitorParamDictService.getInstance().getModel(paramModel);
            String valueRule = paramModel.getValueRule();
            Double dMeas = getValueByRule(valueRule,data.getParamCodeBus(), calStart, cal,dataSource, DataType.minute);
            data.setMeasRe(dMeas);

            //判断参数是否要计算排放量
            if(!"1".equals(paramModel.getCalEmissions())){
                //先获得区间流量的值
                if(dFlow!=null&&dMeas!=null){
                    Double dPl = dFlow * dMeas / 1000;
                    data.setMeasPl(dPl);
                }
            }

            if(data.getMeasRe()!=null){
                resultModels.add(data);
            }
        }

        return resultModels;
    }

    @Override
    public List<Row> getSendData(com.hrt.datasender.datadao.model.BcSendPlatformModel sendPlatForm) {
        Long readySendDatatime = getReadySendDataTime(sendPlatForm);

        if (readySendDatatime == null) {
            return null;
        }

        List<Row> result = getData(sendPlatForm, readySendDatatime);

        return result;
    };

    @Override
    public String getStoageTable() {
        return "BD_S5_MINUTES";
    }

    @Override
    public String getNote() {
        return "";
    }

    @Override
    public String getShowName() {
        return "5分钟数据";
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.MINUTES;
    }

    @Override
    public int getDataInterval() {
        return dataInterval;
    }

    @Override
    public String getConsStr() {
        return "0 0/5 * * * ?";
    }

}
